//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MonteCarloCDOEngine2.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/RandomDefaultModel.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (Cephei::QL::Experimental::Credit::IRandomDefaultModel^ rdm, UInt64 samples) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CRandomDefaultModel^ _Crdm;
    try
    {
#ifdef HANDLE
        _phMonteCarloCDOEngine2 = NULL;
#endif
        _Crdm = safe_cast<CRandomDefaultModel^> (rdm);
        _Crdm->Lock();
        boost::shared_ptr<QuantLib::RandomDefaultModel> _rdm = static_cast<boost::shared_ptr<QuantLib::RandomDefaultModel>> (_Crdm->GetShared ()); 
        QuantLib::Size _samples = (QuantLib::Size)ValueHelper::Convert (samples);
        _ppMonteCarloCDOEngine2 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine2> (new QuantLib::MonteCarloCDOEngine2 ( _rdm,  _samples ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crdm != nullptr) _Crdm->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (boost::shared_ptr<QuantLib::MonteCarloCDOEngine2>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMonteCarloCDOEngine2 = NULL;
#endif
	_ppMonteCarloCDOEngine2 = &childNative;
    
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (QuantLib::MonteCarloCDOEngine2& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMonteCarloCDOEngine2 = NULL;
#endif
	_ppMonteCarloCDOEngine2 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine2> (&childNative);
    
    _MonteCarloCDOEngine2Owner = owner;
    
}

Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (CMonteCarloCDOEngine2^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMonteCarloCDOEngine2 = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMonteCarloCDOEngine2 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine2> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMonteCarloCDOEngine2 = NULL;
#endif
	if (!t->IsSubclassOf(CMonteCarloCDOEngine2::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (QuantLib::Handle<QuantLib::MonteCarloCDOEngine2>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMonteCarloCDOEngine2 = &childNative;
	_ppMonteCarloCDOEngine2 = &static_cast<boost::shared_ptr<QuantLib::MonteCarloCDOEngine2>>(childNative.currentLink());
    
    _MonteCarloCDOEngine2Owner = owner;
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (QuantLib::Handle<QuantLib::MonteCarloCDOEngine2> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMonteCarloCDOEngine2 = &childNative;
	_ppMonteCarloCDOEngine2 = &static_cast<boost::shared_ptr<QuantLib::MonteCarloCDOEngine2>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::CMonteCarloCDOEngine2 (QuantLib::MonteCarloCDOEngine2 childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMonteCarloCDOEngine2 = NULL;
#endif
	_ppMonteCarloCDOEngine2 = new boost::shared_ptr<QuantLib::MonteCarloCDOEngine2> (new QuantLib::MonteCarloCDOEngine2 (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::~CMonteCarloCDOEngine2 ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMonteCarloCDOEngine2 != NULL)
    {
	    delete _ppMonteCarloCDOEngine2;
        _ppMonteCarloCDOEngine2 = NULL;
    }
}
Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::!CMonteCarloCDOEngine2 ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMonteCarloCDOEngine2 != NULL)
    {
	    delete _ppMonteCarloCDOEngine2;
    }
}
QuantLib::MonteCarloCDOEngine2& Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::GetReference ()
{
    if (_ppMonteCarloCDOEngine2 == NULL) throw gcnew NativeNullException ();
	return **_ppMonteCarloCDOEngine2;
}
boost::shared_ptr<QuantLib::MonteCarloCDOEngine2>& Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::GetShared ()
{
    if (_ppMonteCarloCDOEngine2 == NULL) throw gcnew NativeNullException ();
	return *_ppMonteCarloCDOEngine2;
}
QuantLib::MonteCarloCDOEngine2* Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::GetPointer ()
{
    if (_ppMonteCarloCDOEngine2 == NULL) throw gcnew NativeNullException ();
	return &**_ppMonteCarloCDOEngine2;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MonteCarloCDOEngine2>& Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::GetHandle ()
{
	if (_phMonteCarloCDOEngine2 == NULL)
	{
		_phMonteCarloCDOEngine2 = new Handle<QuantLib::MonteCarloCDOEngine2> (*_ppMonteCarloCDOEngine2);
	}
	return *_phMonteCarloCDOEngine2;
}
#endif
bool Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::HasNative () 
{
	return (_ppMonteCarloCDOEngine2 != NULL);
}

Cephei::QL::Experimental::Credit::IMonteCarloCDOEngine2^ Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2::Calculate::get ()
{
    try
    {
    	(*_ppMonteCarloCDOEngine2)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IMonteCarloCDOEngine2^ Cephei::QL::Experimental::Credit::CMonteCarloCDOEngine2_Factory::Create (Cephei::QL::Experimental::Credit::IRandomDefaultModel^ rdm, UInt64 samples)
{
    return gcnew CMonteCarloCDOEngine2 ( rdm,  samples);
}
